
package parser;

import java.util.List;
import java.util.LinkedList;

import lexer.Token;

/**
 * Árbol sintáctico.
 */
public class SyntacticTree {
	
	private final boolean terminal;
	
	private final Token value;
	
	private final List<SyntacticTree> children;
	
	private final Production production;
	
	public SyntacticTree(Token value) {
		this.terminal = true;
		this.production = null;
		this.value = value;
		this.children = new LinkedList<SyntacticTree>();
	}
	
	public SyntacticTree(Production production, List<SyntacticTree> children) {
		this.terminal = false;
		this.production = production;
		this.value = null;
		this.children = children;
	}
	
	public boolean isTerminal() {
		return terminal;
	}
	
	public Token getValue() {
		return value;
	}
	
	public Production getProduction() {
		return production;
	}
	
	public List<SyntacticTree> getChildren() {
		return children;
	}
	
	// TODO eliminar concat. de strings
	public String toString() {
		return toString(0);
	}
	
	private String toString(int deep) {
		String ret = "";
		ret += tabs(deep);
		if(terminal) {
			
			ret += value.toString();
			
		} else {
			ret += production.getHead() + "{\n";
			
			for(SyntacticTree t : children) {
				ret += t.toString(deep + 1);
			}
			
			ret += tabs(deep);
			ret += "}";
		}
		ret += "\n";
		return ret;
	}
	
	private String tabs(int deep) {
		String ret = "";
		for(int i = 0; i < deep; i++) ret += "  ";
		return ret;
	}
}
